import {Component} from '@angular/core';
import {PageVoService} from '../../service/page-vo.service';
import {ActivatedRoute, Router} from '@angular/router';
import {DocService} from '../../service/doc.service';
import {MessageService} from '../../service/message.service';
import {PageVo} from '../../entity/page-vo';
import {LinkDto} from '../../entity/link-dto';
import {formatDate} from '@angular/common';
import {BaseDocComponent} from './base-doc.component';

@Component({
    selector: 'app-page-doc',
    templateUrl: './page-doc.component.html',
    styleUrls: ['./page-doc.component.css']
})
export class PageDocComponent extends BaseDocComponent {
    md: string;

    constructor(protected pageVoService: PageVoService,
                protected activatedRoute: ActivatedRoute,
                protected docService: DocService,
                protected router: Router,
                protected messageService: MessageService) {
        super(activatedRoute, router, pageVoService, docService, messageService);
    }

    /**
     * 当前选中的左菜单
     * @returns {string}
     */
    protected getActiveMenuId(): string {
        if (this.mdId) {
            return this.mdId;
        }
        return null;
    }

    protected onDocReady() {
        if (!this.mdId) {
            this.mdId = '/README';
        }
        if (this.doc.markdownMaps) {
            this.md = this.doc.markdownMaps[this.mdId];
        }
    }

    protected setPageVo(basePageVo: PageVo) {
        if (this.apiVersion) {
            basePageVo.crumbs.push(<LinkDto>{
                text: `${formatDate(this.apiVersion, 'yyyy-MM-dd HH:mm', this.LOCALE)}`,
                routerLink: ['doc', this.docId, 'md', 'README', 'v', this.apiVersion],
                frontIcon: 'history'
            });
        }
        if (this.mdId) {
            basePageVo.crumbs.push(<LinkDto>{
                text: this.mdId.substr(1) + '.md',
                frontIcon: 'bookmark_border'
            });
        }
    }

    /**
     * 获取左侧菜单路由后缀
     * @returns {string[]}
     */
    getLeftMenuRoutes(): string[] {
        if (this.apiVersion) {
            return ['v', this.apiVersion.toString()];
        }
        return null;
    }
}
